package com.pdg.mcplugin.common.baseclasses;

import java.io.File;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

public abstract class PluginBase extends JavaPlugin {
	
	private static final String MESSAGE_CREATED_DATA_FOLDER = "[%s] Created data folder!";
	private static final String ERROR_COULD_NOT_CREATE_DATA_FOLDER = "[%s] Could not create data folder!";
	private static final String MESSAGE_DISABLED_PLUGIN = "[%s] Plugin disabled!";
	private static final String MESSAGE_ENABLED_PLUGIN = "[%s] Plugin enabled!";
	private static final String ERROR_COULD_NOT_ENABLED_PLUGIN = "[%s] Could not enable plugin!";
	private static final String COLOR_MESSAGE = ChatColor.RED.toString();
	private static final String COLOR_SUCCESS = ChatColor.GREEN.toString();
	private static final String COLOR_WARNING = ChatColor.YELLOW.toString();
	private static final String COLOR_INFORMATIONAL = ChatColor.BLUE.toString();
	private static final String COLOR_NONE = "";
	private static final String MESSAGE_FORMAT = "%s[%s] %s";

	//logger for errors
//	private Logger log = Logger.getLogger(LOGGER_NAME);
//	public Logger getLogger(){
//		return log;
//	}

	//ensure data folder exists
	public void ensureDataFolder(){
		//find the folder
		File folder = getDataFolder();
		//does it exist?
		if(!folder.exists()){
			//it doesnt exist
			//try to make it
			if(folder.mkdir()){
				//success
				getLogger().info(String.format(MESSAGE_CREATED_DATA_FOLDER,getName()));
			}else{
				//failure
				getLogger().severe(String.format(ERROR_COULD_NOT_CREATE_DATA_FOLDER,getName()));
			}
		}
	}

	@Override
	public void onDisable() {
		disablePlugin();
		getLogger().info(String.format(MESSAGE_DISABLED_PLUGIN,getName()));
	}

	protected abstract void disablePlugin();
	
	protected abstract boolean enablePlugin();


	//enable event
	@Override
	public void onEnable() {
		if(enablePlugin()){
			getLogger().info(String.format(MESSAGE_ENABLED_PLUGIN,getName()));
		}else{
			getLogger().info(String.format(ERROR_COULD_NOT_ENABLED_PLUGIN,getName()));
		}
	}
	
	public String formatMessage(String theMessage){
		return formatMessage(theMessage,COLOR_NONE);
	}
	
	public String formatMessage(String theMessage, String theColor){
		return String.format(MESSAGE_FORMAT,theColor,getName(),theMessage);
	}

	public void sendErrorMessage(CommandSender theCommandSender, String theMessage){
		theCommandSender.sendMessage(formatMessage(theMessage,COLOR_MESSAGE));
	}
	
	public void sendSuccessMessage(CommandSender theCommandSender, String theMessage){
		theCommandSender.sendMessage(formatMessage(theMessage,COLOR_SUCCESS));
	}
	
	public void sendWarningMessage(CommandSender theCommandSender, String theMessage){
		theCommandSender.sendMessage(formatMessage(theMessage,COLOR_WARNING));
	}
	
	public void sendInformationalMessage(CommandSender theCommandSender, String theMessage){
		theCommandSender.sendMessage(formatMessage(theMessage,COLOR_INFORMATIONAL));
	}
	
	public void sendInformationalMessages(CommandSender theCommandSender, String[] theMessages){
		for(int index=0;index<theMessages.length;++index){
			sendInformationalMessage(theCommandSender,theMessages[index]);
		}
	}
	
	
	public void sendMessage(CommandSender theCommandSender, String theMessage){
		theCommandSender.sendMessage(formatMessage(theMessage,COLOR_NONE));
	}
	
}
